package org.example.controller;

import com.mysql.cj.jdbc.MysqlDataSource;

import javax.sql.DataSource;
import javax.swing.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class MySQL {
    private final static String URL = "jdbc:mysql://127.0.0.1:3306/check?characterEncoding=utf8";
    private final static String USER = "root";
    private final static String PASSWORD = "123456";



    private Connection connection;
    public MySQL() throws SQLException {
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setUrl(URL);
        ((MysqlDataSource)dataSource).setUser(USER);
        ((MysqlDataSource)dataSource).setPassword(PASSWORD);
        this.connection = dataSource.getConnection();

    }
    public Connection getConnection(){
        return this.connection;
    }
    public PreparedStatement getStatement(String sql) throws SQLException {
        return connection.prepareStatement(sql);
    }
    public void close() throws SQLException {
        connection.close();
    }
}